package com.lyf.boot.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;
import org.tio.core.TioConfig;
import org.tio.core.intf.Packet;
import org.tio.core.stat.IpStat;
import org.tio.core.stat.IpStatListener;
import org.tio.utils.json.Json;

/**
 * @author Luyufan
 * @date 2025/1/14
 **/
@Component
@Slf4j
public class TioServerIpStatListener implements IpStatListener {

    @Override
    public void onExpired(TioConfig tioConfig, IpStat ipStat) {
        log.info("可以把统计数据入库\r\n{}", Json.toFormatedJson(ipStat));
    }

    @Override
    public void onAfterConnected(ChannelContext channelContext, boolean b, boolean b1, IpStat ipStat) throws Exception {
        log.info("onAfterConnected\r\n{}", Json.toFormatedJson(ipStat));
    }

    @Override
    public void onDecodeError(ChannelContext channelContext, IpStat ipStat) {
        log.info("onDecodeError\r\n{}", Json.toFormatedJson(ipStat));
    }

    @Override
    public void onAfterSent(ChannelContext channelContext, Packet packet, boolean b, IpStat ipStat) throws Exception {
        log.info("onAfterSent\r\n{}\r\n{}", packet.logstr(), Json.toFormatedJson(ipStat));
    }

    @Override
    public void onAfterDecoded(ChannelContext channelContext, Packet packet, int i, IpStat ipStat) throws Exception {
        log.info("onAfterDecoded\r\n{}\r\n{}", packet.logstr(), Json.toFormatedJson(ipStat));
    }

    @Override
    public void onAfterReceivedBytes(ChannelContext channelContext, int i, IpStat ipStat) throws Exception {
        log.info("onAfterReceivedBytes\r\n{}", Json.toFormatedJson(ipStat));
    }

    @Override
    public void onAfterHandled(ChannelContext channelContext, Packet packet, IpStat ipStat, long l) throws Exception {
        log.info("onAfterHandled\r\n{}\r\n{}", packet.logstr(), Json.toFormatedJson(ipStat));
    }
}
